
package com.dps.omc.service.impl;
import com.dps.omc.enumer.EnumTeam;
import com.dps.omc.model.*;
import com.dps.omc.service.ITaskLogService;
import com.dps.omc.task.message.EnumTaskState;
import com.dps.common.utils.DateUtils;
import com.dps.common.utils.IdGenerator;
import com.dps.omc.dao.ITaskDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dps.common.service.impl.AbstractPageService;
import com.dps.omc.service.ITaskService;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@SuppressWarnings("unchecked")
@Service("TaskServiceImpl")
public class TaskServiceImpl extends AbstractPageService<ITaskDAO,Task> implements ITaskService {

	@Autowired
	private ITaskDAO taskDAO;
	@Autowired
	private ITaskLogService taskLogService;
	@Override
	public boolean getEnableDataPerm() {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public ITaskDAO getDao() {
		return taskDAO;
	}

	@Override
	public Task create(Job job, JobArtifact jobArtifact, Agent agent, AgentNode agentNode, int seq, User lastModified) {
		Task task = new Task();
		task.setId(IdGenerator.createNewId());
		task.setJobId(job.getId());
		task.setAgentId(agent.getId());
		task.setClientIp(agent.getIp());
		task.setBizUnit(agentNode.getBizUnit());
		task.setBizType(jobArtifact.getBizType());
		task.setGroupType(jobArtifact.getGroupType());
		task.setStatus(EnumTaskState.NEW);
		task.setNote(jobArtifact.getArtifactName()+"@"+agentNode.getAlias());
		task.setIsTimer(job.getIscron());
		task.setPlanDate(agentNode.getPlanDate());
		task.setTemplateId(jobArtifact.getTemplateId());
		task.setArtifactId(jobArtifact.getArtifactId());
		task.setSeq(seq);
		task.setCreaterId(job.getCreaterId());
		task.setCreaterName(job.getCreaterName());
		task.setTeam(agentNode.getTeam());
		task.setNodeId(agentNode.getNodeId());
		task.setLastModifiedId(lastModified.getId());
		task.setLastModifiedName(lastModified.getRealName());//调用User中的RealName，创建User对象其中的属性
		this.add(task,lastModified);
//		taskLogService.createLog(task);
		return task;
	}
	@Override
	public Task add(Task task,User lastModified) {
		if((StringUtils.isEmpty(task.getId())))
		{
			task.setId(IdGenerator.createNewId());
		}
		getDao().insert(task);
		task.setLastModifiedId(lastModified.getId());
		task.setLastModifiedName(lastModified.getRealName());
		taskLogService.createLog(task,lastModified);
		return task;
	}

	@Override
	public void update(Task task,User lastModified) {
		task.setLastModifiedId(lastModified.getId());
		task.setLastModifiedName(lastModified.getRealName());
		getDao().update(task);
		taskLogService.createLog(task,lastModified);
	}

	@Override
	public boolean allFinish(Job job) {

		return getDao().findNewByJob(job.getId()).size()>0?false:true;
//		return this.findUnfinishAndExpiredByJob(job).size()>0?false:true;true
	}

	@Override
	public List<Task> findUnfinishAndExpiredByJob(Job job) {
		String now = DateUtils.currentDatetime();
		return getDao().findUnfinishAndExpiredByJob(job.getId(),now);
	}

	@Override
	public List<Task> findNewByJob(Job job) {
		return getDao().findNewByJob(job.getId());
	}

	@Override
	public List<Task> findNewByJobAndTeam(Job job, EnumTeam team) {
		return getDao().findNewByJobAndTeam(job.getId(),team.getValue());
	}

	@Override
	public List<Task> findListByJobId(String jobId) {
		return getDao().findListByJobId(jobId);
	}

	@Override
	public List<Task> queryByStatusAndStartDate(EnumTaskState status ,Date queryDate){
		return getDao().queryByStatusAndStartDate(status,queryDate);
	}
}
